<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\BidModel;
use app\index\model\BidPoolModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceLinModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginChaModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginXunModel;
use app\index\model\EnginZhengModel;
use app\index\model\FlowBillModel;
use app\index\model\HouGonghuiModel;
use app\index\model\HouGongshuiModel;
use app\index\model\HouGongzhiQuModel;
use app\index\model\HouKouFuwuModel;
use app\index\model\HouKouKuaifeiModel;
use app\index\model\HouKouQifeiModel;
use app\index\model\HouKouShuifeiModel;
use app\index\model\IncomeEnginModel;
use app\index\model\KuaiJiModel;
use app\index\model\KuaiShouModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\StuffJieModel;
use app\index\model\SubbaoShenModel;
use app\index\model\UserModel;
use app\index\service\BidService;
use app\index\service\BudgetService;
use app\index\service\CertService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\HouService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\PiaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Home extends Common
{

    public function engin_chan(Request $request){
        $param = $request->post();
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $gui_unit = $request->param('gui_unit', 0, 'int');
        if(!$gui_unit){
            $gui_unit = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $gui_unit_pid = 0;
        }else{
            $gui_unit_pid = DeptModel::where(['id'=>$gui_unit])->value(('pid'));
            $gui_unit_son = (new UserService())->get_chansonid($gui_unit);
        }
        $whereChan = [];
        if($gui_unit_pid > 0){
            $whereChan[] = ['gui_unit','in', $gui_unit_son];
        }

        $enginIdArr = EnginModel::where($where)->where($whereTurn)->where($whereChan)->where((new MenuService())->get_miss_where('homeengin', ''))->column('id');
        $data['tong_all'] = count($enginIdArr);
        $data['tong_cheng'] = EnginModel::where($where)->where($whereTurn)->where($whereChan)->where((new MenuService())->get_miss_where('homeengin', ''))->where(['state'=>1])->count();
        $data['tong_zai'] = EnginModel::where($where)->where($whereTurn)->where($whereChan)->where((new MenuService())->get_miss_where('homeengin', ''))->where(['state'=>3])->count();
        $data['tong_jun'] = EnginModel::where($where)->where($whereTurn)->where($whereChan)->where((new MenuService())->get_miss_where('homeengin', ''))->where(['state'=>5])->count();
        $data['tong_wan'] = EnginModel::where($where)->where($whereTurn)->where($whereChan)->where((new MenuService())->get_miss_where('homeengin', ''))->where(['state'=>7])->count();

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = $gui_unit;
        $data['enginidstr'] = implode(',', $enginIdArr);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function engin_dong(Request $request){
        $dt = $request->param('dt', '', 'string');
        $dt = empty($dt) ? date('Y-m-d') : $dt;

        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['created_at','between', [$dt.' 00:00:00', $dt.' 23:59:59']];

        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->where('enginid','in',$enginidstr)->order('created_at DESC')->select()->toArray();
        foreach ($list as &$vvv){
            $vvv['created_time'] = date("H:i", strtotime($vvv['created_at']));
        }

        $data['data'] = $list;
        $data['isdt'] = $dt;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function engin_dan(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        $enginid = $request->param('enginid', 0, 'int');
        $engininfo = [];

        $enginlist = EnginModel::field('*,id as value')->where('id', 'in', $enginidstr)->order('id DESC')->select()->toArray();
        if(!$enginid){
            $enginid = isset($enginlist[0]['id']) ? $enginlist[0]['id'] : 0;
            $engininfo = isset($enginlist[0]) ? $enginlist[0] : [];
        }else{
            $engininfo = EnginModel::where(['id'=>$enginid])->find();
        }
        $data['enginlist'] = $enginlist;
        $data['is_enginid'] = $enginid;
        $data['isinfo'] = false;
        if($engininfo == []){
            return outputJson(2000,lang('code_2000'),$data);
        }

        $data['isinfo'] = true;
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $whereEngin[] = ['enginid','=', $engininfo['id']];


        $data['data'] = EnginModel::get_info_by_pk($engininfo['id']);
        $data['data']['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['name'=>$data['data']['gofuer']])->value('liantel');

        $data['data']['lantake_name'] = (new EnginService())->get_engin_lantake_name($data['data']['lantake']);
        $data['data']['state_name'] = (new EnginService())->get_engin_state_name($data['data']);
        $data['data']['state_name_wen'] = (new EnginService())->get_engin_state_name_tag($data['data']);
        $data['data']['win_unit_name'] = DeptModel::where(['id'=>$data['data']['win_unit']])->value('name');
        $data['data']['gui_unit_name'] = DeptModel::where(['id'=>$data['data']['gui_unit']])->value('name');

        $data['data']['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $data['data']['lanshi']);
        $data['data']['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $data['data']['kailei']);
        $data['data']['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $data['data']['zhaotype']);


        $data['neiinfo'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->find();
        if($data['neiinfo']){
            $data['neiinfo']['state_name'] = (new ContractService())->get_contract_nei_state_name($data['neiinfo']['state']);
        }

        $data['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->find();
        if($data['shiinfo']){
            $data['shiinfo']['bumoney'] = ContractBuModel::where(['shiid'=>$data['shiinfo']['id'],'deleted_user'=>0])
                ->where(['status'=>1,'isturn'=>1])->sum('money');
            $data['shiinfo']['money'] += $data['shiinfo']['bumoney'];
        }

        $data['shijiemoney'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->sum('money');


        $data['piaokaifee'] = PiaoKaiListModel::alias('list')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
            ->where(['kai.enginid'=>$data['data']['id']])
            ->sum('list.money');

        $data['gonghuimoney'] = (new HouService())->get_hou_gonghui_money($data['data']['id']);

        $data['huinokai'] = round($data['gonghuimoney'] - $data['piaokaifee'], 2);
        $data['huinokai'] = $data['huinokai'] > 0 ? $data['huinokai'] : 0;

        $data['kainohui'] = round($data['piaokaifee'] - $data['gonghuimoney'], 2);
        $data['kainohui'] = $data['kainohui'] > 0 ? $data['kainohui'] : 0;

        //当前工程的工程销项票金额÷施工合同金额（结算优先）×100%
        $data['leixiaobi'] = 0;
        if($data['shijiemoney'] > 0){
            $data['leixiaobi'] = round($data['piaokaifee'] / $data['shijiemoney'] * 100, 2);
        }else{
            if($data['shiinfo'] && $data['shiinfo']['money'] > 0){
                $data['leixiaobi'] = round($data['piaokaifee'] / $data['shiinfo']['money'] * 100, 2);
            }else{
                if($data['data']['winfee'] > 0){
                    $data['leixiaobi'] = round($data['piaokaifee'] / $data['data']['winfee'] * 100, 2);
                }
            }
        }

        //累计工程回款÷施工合同金额（结算优先）X100%
        $data['leihuibi'] = 0;
        if($data['shijiemoney'] > 0){
            $data['leihuibi'] = round($data['gonghuimoney'] / $data['shijiemoney']*100, 2);
        }else{
            if($data['shiinfo'] && $data['shiinfo']['money'] > 0){
                $data['leihuibi'] = round($data['gonghuimoney'] / $data['shiinfo']['money']*100, 2);
            }else{
                if($data['data']['winfee'] > 0){
                    $data['leihuibi'] = round($data['gonghuimoney'] / $data['data']['winfee']*100, 2);
                }
            }
        }

        $data['gongzhimoney'] = (new HouService())->get_hou_gongzhi_money($data['data']['id']);
        $data['gongzhimoneygogong'] = (new HouService())->get_hou_gongzhi_money_gogong($data['data']['id']);

        $data['piaoshoumoney'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->sum('money');
        $data['piaoshouratefee'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->sum('ratefee');

        $data['contractcaimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->sum('money');
        $data['contractcaijiemoney'] = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->sum('money');

        $data['hougongshuimoney'] = HouGongshuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->sum('money');

        //施工合同金额（结算优先）X劳务分包服务费率÷100
        $data['yingkoufuwu'] = 0;
        if($data['neiinfo'] && $data['neiinfo']['ratefu']){
            if($data['shijiemoney'] > 0){
                $data['yingkoufuwu'] = round($data['shijiemoney'] * $data['neiinfo']['ratefu'] / 100, 2);
            }else{
                if($data['shiinfo'] && $data['shiinfo']['money'] > 0){
                    $data['yingkoufuwu'] = round($data['shiinfo']['money'] * $data['neiinfo']['ratefu'] / 100, 2);
                }
            }
        }

        $data['yikoufuwu'] = HouKouFuwuModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1, 'list.enginid'=>$data['data']['id']])
            ->sum('list.koufee');
        $data['yikoufuwu'] += HouKouFuwuModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'list.enginid'=>$data['data']['id']])
            ->sum('list.koufee');

        //工程销项票金额（元）X税款保证金费率 ÷100
        $data['yingkoushuifei'] = 0;
        if($data['neiinfo'] && $data['neiinfo']['rateshui'] && $data['piaokaifee']){
            $data['yingkoushuifei'] = round($data['piaokaifee'] * $data['neiinfo']['rateshui'] / 100, 2);
        }
        $data['yikoushuifei'] = HouKouShuifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->join('engin_shui shui','shui.id=list.shuiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
            ->where(['list.type'=>1, 'shui.enginid'=>$data['data']['id']])
            ->sum('list.koufee');

        $data['yikoushuifei'] += HouKouShuifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->join('engin_shui shui','shui.id=list.shuiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
            ->where(['list.type'=>2, 'shui.enginid'=>$data['data']['id']])
            ->sum('list.koufee');


        $data['yingkuaifei'] = KuaiJiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id'],'isgua'=>'是'])->sum('kuaifee');
        $data['yingkuaifei'] += KuaiShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id'],'isgua'=>'是'])->sum('kuaifee');

        $data['yikuaifei'] = HouKouKuaifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->join('kuai_ji kuai','kuai.id=list.kuaiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['kuai.comid'=>$this->userinfo['comid'],'kuai.deleted_user'=>0,'kuai.isturn'=>1,'kuai.status'=>1])
            ->where(['list.type'=>2,'list.kuaitype'=>1, 'kuai.enginid'=>$data['data']['id']])
            ->sum('list.koufee');

        $data['yikuaifei'] += HouKouKuaifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->join('kuai_shou kuai','kuai.id=list.kuaiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['kuai.comid'=>$this->userinfo['comid'],'kuai.deleted_user'=>0,'kuai.isturn'=>1,'kuai.status'=>1])
            ->where(['list.type'=>2,'list.kuaitype'=>2, 'kuai.enginid'=>$data['data']['id']])
            ->sum('list.koufee');


        $data['yiqifei'] = HouKouQifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1, 'list.enginid'=>$data['data']['id']])
            ->sum('list.koufee');

        $data['yiqifei'] += HouKouQifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'list.enginid'=>$data['data']['id']])
            ->sum('list.koufee');

        $data['leikoushoufee'] = round($data['yikoufuwu'] + $data['yikoushuifei'] + $data['yikuaifei'] + $data['yiqifei'], 2);



        $data['gongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($data['data']['id']);
        $data['gonghuikoufee'] = HouGonghuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['id']])->sum('koufee');


        $data['jiedongkoufee'] = HouKouQifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1, 'list.enginid'=>$data['data']['id'],'isdong'=>1])
            ->sum('list.koufee');

        $data['jiedongkoufee'] += HouKouQifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'list.enginid'=>$data['data']['id'],'isdong'=>1])
            ->sum('list.koufee');

        $data['gonghui_keyu'] = round($data['gonghuimoney'] - $data['gongzhimoneyquben'] - $data['gonghuikoufee'] + $data['jiedongkoufee'], 2);


        $data['gongshouzhicha'] = round($data['gonghuimoney'] - $data['gongzhimoney'], 2);

        $data['cha_dainum'] = EnginChaModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['isxu'=>'是', 'iszheng'=>'否', 'enginid'=>$data['data']['id']])
            ->count();


        $data['gongzhimoney_dang'] = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['qu.type'=>2, 'zhi.enginid'=>$data['data']['id']])
            ->sum('qu.money');

        $data['gongzhimoney_yufu'] = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['qu.type'=>1, 'zhi.enginid'=>$data['data']['id']])
            ->sum('qu.money');

        $data['gongzhimoney_qita'] = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['qu.type'=>3, 'zhi.enginid'=>$data['data']['id']])
            ->sum('qu.money');



        $data['gongzhimoneygogong_dang'] = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->join('dealcom deal','deal.name=list.fuer')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['deal.type'=>3,'qu.type'=>2, 'zhi.enginid'=>$data['data']['id']])
            ->sum('qu.money');

        $data['gongzhimoneygogong_yufu'] = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->join('dealcom deal','deal.name=list.fuer')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['deal.type'=>3,'qu.type'=>1, 'zhi.enginid'=>$data['data']['id']])
            ->sum('qu.money');
        $data['gongzhimoneygogong_qita'] = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->join('dealcom deal','deal.name=list.fuer')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['deal.type'=>3,'qu.type'=>3, 'zhi.enginid'=>$data['data']['id']])
            ->sum('qu.money');

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function cai_chan(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }


        $stadt = date('Y-m-01');
        $enddt = date('Y-m-d', strtotime("$stadt +1 month -1 day"));

        $whereCom[] = ['comid', '=', $this->userinfo['comid']];
        $whereCom[] = ['deleted_user', '=', 0];
        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $ListWhereChan = [];
        $WhereChan = [];
        if($chanid_pid > 0){
            $ListWhereChan[] = ['acc.chanid','in', $chanid_son];
            $WhereChan[] = ['chanid','in', $chanid_son];
        }
        $data['account_shou'] = AccountListModel::alias('list')
            ->join('account acc','acc.id=list.accid')
            ->where(['list.comid'=>$this->userinfo['comid']])
            ->where('list.type','in', [1,2,8,9,11,14])
            ->where('list.dt','between', [$stadt, $enddt])
            ->where($ListWhereChan)->sum('money');
        $data['account_zhi'] = AccountListModel::alias('list')
            ->join('account acc','acc.id=list.accid')
            ->where(['list.comid'=>$this->userinfo['comid']])
            ->where('list.type','not in', [1,2,8,9,11,14])
            ->where('list.dt','between', [$stadt, $enddt])
            ->where($ListWhereChan)->sum('money');

        $data['piao_kai'] = PiaokaiModel::where($whereCom)->where($WhereChan)->where($whereTurn)
            ->where('dt','between', [$stadt, $enddt])->sum('money');
        $data['piao_shou'] = PiaoShouModel::where($whereCom)->where($WhereChan)->sum('money');



        $daizhi = [];
        //3-项目支付 4-费用报销 5-日常付款 6-内包付款 7-保证金缴纳 10保证金返还 13-借支 15-公司费用报销
        $info = PaymentEnginModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>3,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目付款（销售方）';
                $v['laipage'] = 'payment_engin';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentBaoModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>4,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目费用报销';
                $v['laipage'] = 'payment_bao';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentDayModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>5,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '日常付款';
                $v['laipage'] = 'payment_day';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentNeifuModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>6,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目内包付款';
                $v['laipage'] = 'payment_neifu';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = EnginBondJiaoModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>7,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目保证金缴纳';
                $v['laipage'] = 'engin_bond_jiao';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = EnginBondFanModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>10,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目保证金返还';
                $v['laipage'] = 'engin_bond_fan';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentJieModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>13,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '备用金申请';
                $v['laipage'] = 'payment_jie';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentGongModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>15,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '公司费用报销';
                $v['laipage'] = 'payment_gong';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }

        $dtSort = array_column($daizhi,'created_at');
        array_multisort($dtSort,SORT_DESC,$daizhi);
        $data['daizhi'] = $daizhi;

        $daibao = EnginBondKaiModel::where($whereCom)->where($whereTurn)->where($WhereChan)
            ->where("kaidt is null or kaidt = '0000-00-00' or kaidt = ''")->select()->toArray();
        foreach ($daibao as &$v){
            if($v['jiedt'] != '0000-00-00' && $v['jiedt']){
                $v['jiedt'] = date("Y-m-d H:i", strtotime($v['jiedt']));
            }else{
                $v['jiedt'] = '';
            }
            $bill = FlowBillModel::where(['id'=>$v['billid']])->find();
            $v['sericnum'] = $bill['sericnum'];
            $v['bondtype'] = (new PaymentService())->get_engin_bondtype_name($v['bondtype']);
            $v['hantype'] = (new OptService())->get_base_showname('engin_bond_hantype', $v['hantype']);
        }
        $data['daibao'] = $daibao;



        $daipiao = PiaokaiModel::where($whereCom)->where($whereTurn)->where($WhereChan)
            ->where("dt is null or dt = '0000-00-00' or dt = ''")->order('created_at DESC')->select()->toArray();
        foreach ($daipiao as &$v){
            $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
            $v['enginname'] = EnginModel::where(['id'=>$v['enginid']])->value('name');
            $v['piaotype'] = (new PiaoService())->get_shou_piaotype_name($v['piaotype']);
        }
        $data['daipiao'] = $daipiao;




        $dairen = IncomeEnginModel::where($whereCom)->where($WhereChan)->where(['mal'=>0])->select()->toArray();
        $data['dairen'] = $dairen;



        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_dong(Request $request){
        $dt = $request->param('dt', '', 'string');
        $dt = empty($dt) ? date('Y-m-d') : $dt;

        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }

        $WhereChan = [];
        if($chanid_pid > 0){
            $WhereChan[] = ['chanid','in', $chanid_son];
        }

        $list = AccountListModel::alias('list')
            ->join('account acc','acc.id=list.accid')
            ->where(['list.comid'=>$this->userinfo['comid']])
            ->where('list.dt','=', $dt)
            ->where($WhereChan)->select()->toArray();
        foreach ($list as &$v){
            $v['type_name'] = in_array($v['type'], [1,2,8,9,11,14]) ? '到账' : '付款';
        }

        $data['data'] = $list;
        $data['isdt'] = $dt;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function cert_chan(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
            $chanid_son = [];
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }
        $where[] = ['comid', '=', $this->userinfo['comid']];
        $where[] = ['deleted_user', '=', 0];


        $data['person_num'] = CertModel::where($where)->group('personid')->count();
        $data['use_person_num'] = CertPersonModel::where($where)->where(['statueuse'=>2])->count();
        $data['free_person_num'] = CertPersonModel::where($where)->where(['statueuse'=>1])->count();
        $data['cert_num'] = CertModel::where($where)->count();
        $data['cert_bor_num'] = CertModel::where($where)->where(['goes'=>2])->count();
        $data['cert_bid_num'] = CertModel::where($where)->where(['statueuse'=>3])->count();
        $data['cert_bao_num'] = CertModel::where($where)->where('statueuse','in',[5,7])->count();
        $data['cert_suo_num'] = CertModel::where($where)->where('statueuse','=',7)->count();
        $data['cert_free_num'] = CertModel::where($where)->where('statueuse','=',1)->count();


        // 借证申请待处理
        $borrow_data = CertBorrowModel::where($where)->where(['isturn'=>1,'status'=>0,'deleted_user'=>0])
            ->field('flowuser,flowdept,yong,created_at,state')->order('created_at desc')->select()->toArray();
        $data['borrow_num'] = count($borrow_data);
        foreach ($borrow_data as &$item){
            $item['state_name'] = (new CertService())->get_cert_borrow_state_name($item['state']);
        }
        $data['borrow_data'] = $borrow_data;
        unset($item);

        // 逾期为归还证书
        $borlist_data = CertBorrowListModel::alias('list')->field('list.certid,bor.flowuser,bor.qudt,bor.yudt')
            ->leftJoin('cert_borrow bor','bor.id = list.mid')
            ->where("list.guidt = '' or list.guidt is null or list.guidt ='0000-00-00'")
            ->where(['bor.state'=>1,'bor.status'=>1,'bor.isturn'=>1,'list.type'=>1])
            ->where(['bor.deleted_user'=>0,'bor.comid'=>$this->userinfo['comid']])
            ->order('bor.qudt asc')
            ->select()->toArray();
        $data['borlist_num'] = count($borlist_data);
        foreach ($borlist_data as &$item){
            $item['cert_name'] = CertModel::where('id',$item['certid'])->value('name');
            $cert_major = CertModel::where('id',$item['certid'])->value('major');
            $cert_major = str_replace('[]',',',$cert_major);
            if(!empty($cert_major)){
                $item['cert_name'] = $item['cert_name'].' / '.$cert_major;
            }
            $item['day'] = get_time_slot_days($item['yudt'],date('Y-m-d'));
        }
        $data['borlist_data'] = $borlist_data;
        unset($item);

        // 投标用证待处理
        $biduse_data = CertUseBidModel::where($where)->where(['isturn'=>1,'status'=>0,'deleted_user'=>0])
            ->field('flowuser,flowdept,bidid,dt')
            ->select()->toArray();
        $data['biduse_num'] = count($biduse_data);
        foreach ($biduse_data as &$item){
            $item['name'] = BidModel::where('id',$item['bidid'])->value('name');
        }
        $data['biduse_data'] = $biduse_data;
        unset($item);

        // 报建用证待处理
        $enginuse_data = CertUseModel::where($where)->where(['isturn'=>1,'status'=>0,'deleted_user'=>0])
            ->field('flowuser,flowdept,enginid,dt')
            ->select()->toArray();
        $data['enginuse_num'] = count($enginuse_data);
        foreach ($enginuse_data as &$item){
            $item['name'] = EnginModel::where('id',$item['enginid'])->value('name');
        }
        $data['enginuse_data'] = $enginuse_data;
        unset($item);


        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cert_dong(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }

        $where[] = ['comid', '=', $this->userinfo['comid']];
        $where[] = ['deleted_user', '=', 0];
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }

        $cert_data = CertModel::where($where)->select()->toArray();
        $list3 = $list6 = $list9 = $list1 = [];
        foreach ($cert_data as $item){
            $person_name = CertPersonModel::where('id',$item['personid'])->value('name');
            $expiry_dt = array_filter(explode('[]',$item['expiry_dt']));
            $major = array_filter(explode('[]',$item['major']));
            if($expiry_dt){
                $date = date('Y-m-d');
                foreach ($expiry_dt as $k => $dt){
                    $dt3 = date('Y-m-d',strtotime($dt."-30 day"));
                    $dt6 = date('Y-m-d',strtotime($dt."-60 day"));
                    $dt9 = date('Y-m-d',strtotime($dt."-90 day"));
                    if($date >= $dt3 && $date <= $dt){
                        if(isset($major[$k])) {
                            $list3[$dt][] = $item['name'] . '[' . $major[$k] . ']' . $person_name;
                        }
                    }
                    if($date >= $dt6 && $date <= $dt){
                        if(isset($major[$k])) {
                            $list6[$dt][] = $item['name'] . '[' . $major[$k] . ']' . $person_name;
                        }
                    }
                    if($date >= $dt9 && $date <= $dt){
                        if(isset($major[$k])) {
                            $list9[$dt][] = $item['name'] . '[' . $major[$k] . ']' . $person_name;
                        }
                    }
                    if($date > $dt){
                        if(isset($major[$k])){
                            $list1[$dt][] = $item['name'].'['.$major[$k].']'.$person_name;
                        }
                    }
                }
            }
        }

        ksort($list3);
        ksort($list6);
        ksort($list9);
        ksort($list1);
        $data['list3'] = $list3;
        $data['list6'] = $list6;
        $data['list9'] = $list9;
        $data['list1'] = $list1;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function bid_chan(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
            $chanid_son = [];
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }
        $where[] = ['comid', '=', $this->userinfo['comid']];
        $where[] = ['deleted_user', '=', 0];


        $data['pool_num'] = BidPoolModel::where($where)->count();
        $data['shen_num'] = BidModel::where($where)->where(['status'=>0,'isturn'=>1])->count();
        $data['bid_num'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1,'state'=>1])->count();
        $data['win_num'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1,'state'=>3])->count();
        $data['nowin_num'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1,'state'=>6])->count();


        // 最近截标项目
        $data['bidjie_data'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1])->where('enddt_tou','>=',date('Y-m-d'))->order('enddt_tou asc')
            ->field('name,business,zhaoname,enddt_tou')->limit(5)->select()->toArray();
        foreach ($data['bidjie_data'] as &$item){
            $item['business'] = $item['business'] == 1 ? '自营' : '联营';
        }
        unset($item);

        // 最近待开标项目
        $data['biddai_data'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1])->where(['state'=>1])->where('kaidt','>=',date('Y-m-d'))
            ->order('kaidt asc')->field('name,business,zhaoname,kaidt')->limit(5)->select()->toArray();

        // 最近已开标项目
        $data['bidkai_data'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1])->where('state','in',[3,6])
            ->where('kaidt','<=',date('Y-m-d'))
            ->order('kaidt asc')->field('name,business,zhaoname,status,state,isqi')->limit(5)->select()->toArray();
        foreach ($data['bidkai_data'] as &$item){
            $item['business'] = $item['business'] == 1 ? '自营' : '联营';
            $item['result'] = (new BidService())->get_bid_state_name_wen($item);
        }
        unset($item);

        // 最近中标项目
        $data['bidwin_data'] = BidModel::where($where)->where('kai_windt','<=',date('Y-m-d'))
            ->where(['status'=>1,'isturn'=>1])->where('state','=',3)
            ->order('kai_windt desc')->field('name,business,zhaoname,kai_fee')->limit(5)->select()->toArray();
        foreach ($data['bidwin_data'] as &$item){
            $item['business'] = $item['business'] == 1 ? '自营' : '联营';
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function bid_dong(Request $request){
        $dt = $request->param('dt', '', 'string');
        $dt = empty($dt) ? date('Y-m-d') : $dt;

        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
            $chanid_son = [];
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['created_at','between', [$dt.' 00:00:00', $dt.' 23:59:59']];

        $flow_list = [];
        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->order('created_at DESC')->where('modenum','bid')->select()->toArray();
        foreach ($list as $v){
            $v['created_time'] = date("H:i", strtotime($v['created_at']));
            $v['name'] = $v['summary'];
            $flow_list[] = $v;
        }

        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->order('created_at DESC')->where('modenum','in',['engin_bond_jiao','engin_bond_fan','engin_bond_kai'])
            ->where('bidid','>',0)->select()->toArray();
        foreach ($list as $v){
            $v['created_time'] = date("H:i", strtotime($v['created_at']));
            $v['name'] = $v['summary'];
            $flow_list[] = $v;
        }

        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->order('created_at DESC')->where(['modenum'=>'cert_use'])
            ->select()->toArray();
        foreach ($list as $v){
            $type = CertUseModel::where('id',$v['mid'])->value('type');
            if($type == 1){
                $v['created_time'] = date("H:i", strtotime($v['created_at']));
                $v['name'] = $v['summary'];
                $flow_list[] = $v;
            }
        }

        $data['data'] = $flow_list;
        $data['isdt'] = $dt;
        return outputJson(2000,lang('code_2000'),$data);
    }


}















